package com.huijutao;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huijutao.CustomView.WebViewSetting;
import com.huijutao.beans.GroupItem;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.List;


public class GroupActivity extends ActionBarActivity {
    public static final String DATA = "data";
    private GroupItem groupItem;
    private String groupItemTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //设置Up按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //获取数据
        groupItem = (GroupItem) getIntent().getSerializableExtra(DATA);
        groupItemTitle = groupItem.getTitle();
        ImageView img = (ImageView) findViewById(R.id.img);
        TextView txt_title = (TextView) findViewById(R.id.txt_title);
        TextView txt_price = (TextView) findViewById(R.id.txt_price);
        TextView txt_attend = (TextView) findViewById(R.id.txt_attend);
        MyApp.mImageXutils.display(img, groupItem.getImg_path());
        txt_title.setText(groupItem.getTitle());
        txt_price.setText("¥" + groupItem.getPrice());
        txt_attend.setText(groupItem.getAttend_count() + "人参加");
        //设置去购买的点击事件
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse(groupItem.getGo_url());
                Intent it = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(it);
            }
        });
        //设置WebView
        WebView webView_main = (WebView) findViewById(R.id.wv);
        WebViewSetting.setWebView(webView_main);
        webView_main.loadDataWithBaseURL(null, groupItem.getMemo(), "text/html", "UTF-8", null);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_groupactivity,menu);
        MenuItem item = menu.findItem(R.id.shoucang);
        try {
            List<GroupItem> data = MyApp.dbUtils.findAll(Selector.from(GroupItem.class).where("title","=", groupItemTitle));
            //Toast.makeText(this,data.toString(),Toast.LENGTH_SHORT).show();
            if (data !=null&&data.size()>0){
                item.setIcon(R.drawable.bj_chk_collect_checked);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        MenuItem share = menu.findItem(R.id.menu_findactivity_share);
        ShareActionProvider actionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(share);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/*");
        intent.putExtra(Intent.EXTRA_TITLE,"分享");
        intent.putExtra(Intent.EXTRA_TEXT,groupItem.getTitle());
        actionProvider.setShareIntent(intent);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        try {
            if (id == android.R.id.home) {
                this.finish();
            }
            if (id==R.id.shoucang){
                List<GroupItem> data=MyApp.dbUtils.findAll(Selector.from(GroupItem.class).where("title","=", groupItemTitle));
                //Toast.makeText(this,data.toString(),Toast.LENGTH_SHORT).show();
                if (data==null||data.size()==0) {
                        item.setIcon(R.drawable.bj_chk_collect_checked);
                        Toast.makeText(this,"收藏成功",Toast.LENGTH_SHORT).show();
                        //Toast.makeText(this,groupItem.toString(),Toast.LENGTH_SHORT).show();
                        MyApp.dbUtils.saveOrUpdate(groupItem);
                }else {
                        item.setIcon(R.drawable.bj_chk_collect_default);
                        MyApp.dbUtils.delete(GroupItem.class, WhereBuilder.b("title","=", groupItemTitle));
                        Toast.makeText(this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return super.onOptionsItemSelected(item);
    }
}
